import { message } from 'ant-design-vue'
import { $fetch } from 'ohmyfetch'
import type { FetchRequest, FetchOptions } from 'ohmyfetch'
import type { IApiBase } from '../types/api'

export const baseUrl = 'http://127.0.0.1:8081/api'

const _useApi = $fetch.create({
    baseURL: baseUrl,
    //请求拦截器
    async onRequest() {
        //请求头传如token
    },
    //响应拦截器
    async onResponse({response}) {
        //接口请求异常捕获
        const data = response._data
        if ( data.code !== 0) {
            if (data.code === 270004) return;
            if (data.code === 250004) return;
            if (data.code === 280001) return;
            if (data.code === 400001) return;
            message.error(data.msg)
        }
    },
})

export const useApi = async <T = any> (
    request: FetchRequest,
    options?: FetchOptions<'json'>
) => {
    return await _useApi<IApiBase<T>>(request, options)
}

